package com.dominator.serviceImpl;

import com.dominFramework.core.id.SystemIdUtils;
import com.dominFramework.core.typewrap.Dto;
import com.dominFramework.core.utils.SystemUtils;
import com.dominator.enums.ReqEnums;
import com.dominator.mybatis.dao.T_userPO;
import com.dominator.mybatis.dao.T_user_bankDao;
import com.dominator.mybatis.dao.T_user_bankPO;
import com.dominator.repository.DaoBank;
import com.dominator.repository.DaoUser;
import com.dominator.service.ApiBankService;
import com.dominator.utils.api.ApiMessage;
import com.dominator.utils.api.ApiMessageUtil;
import com.dominator.utils.api.ApiUtils;
import com.dominator.utils.exception.ApiException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class ApiBankServiceImpl implements ApiBankService {

    @Autowired
    T_user_bankDao tUserBankDao;
    @Autowired
    DaoUser userDao;
    @Autowired
    DaoBank bankDao;


    @Override
    public ApiMessage addBank(Dto dto) throws ApiException {
        ApiUtils.checkParam(dto,"phoneNum,bankNum,bankId,realName");
        T_userPO user = userDao.selectByPhoneNum(dto.getString("phoneNum"));
        if (SystemUtils.isEmpty(user)) {
            throw new ApiException(ReqEnums.REQ_USER_NOT_EXSIT);
        } else if (!"1".equals(user.getIs_valid())) {
            throw new ApiException(ReqEnums.REQ_USER_IS_NOT_VALID);
        }

        T_user_bankPO t_user_bankPO=bankDao.queryBank(dto);
        if (SystemUtils.isNotEmpty(t_user_bankPO)){
            throw new ApiException(ReqEnums.BANK_IS_EXSIT);
        }
        T_user_bankPO tUserBankPO =new T_user_bankPO();
        tUserBankPO.setId(SystemIdUtils.uuid());
        tUserBankPO.setUser_id(user.getId());
        tUserBankPO.setBank_id(dto.getString("bankId"));
        tUserBankPO.setBank_num(dto.getString("bankNum"));
        tUserBankPO.setReal_name(dto.getString("realName"));
        tUserBankPO.setCreate_time(new Date());
        tUserBankPO.setIs_valid("1");
        tUserBankPO.setProvince(dto.getString("province"));
        tUserBankPO.setCity(dto.getString("city"));
        tUserBankDao.insert(tUserBankPO);
        return ApiMessageUtil.success();
    }

    @Override
    public ApiMessage getUserBankList(Dto dto) throws ApiException {
        ApiUtils.checkParam(dto,"phoneNum");
        T_userPO user = userDao.selectByPhoneNum(dto.getString("phoneNum"));
        if (SystemUtils.isEmpty(user)) {
            throw new ApiException(ReqEnums.REQ_USER_NOT_EXSIT);
        } else if (!"1".equals(user.getIs_valid())) {
            throw new ApiException(ReqEnums.REQ_USER_IS_NOT_VALID);
        }
        return ApiMessageUtil.success(bankDao.queryBankList(user.getId()));
    }

    @Override
    public ApiMessage deleteBank(Dto dto) throws ApiException {
        ApiUtils.checkParam(dto,"bankId");
        T_user_bankPO tUserBankPO=tUserBankDao.selectByKey(dto.getString("bankId"));
        if (SystemUtils.isEmpty(tUserBankPO)){
            throw new ApiException(ReqEnums.BANK_IS_NOT_EXSIT);
        }else if(!"1".equals(tUserBankPO.getIs_valid())){
            throw new ApiException(ReqEnums.BANK_IS_NOT_VALID);
        }
        tUserBankPO.setIs_valid("0");
        tUserBankPO.setModify_time(new Date());
        tUserBankDao.updateByKey(tUserBankPO);
        return ApiMessageUtil.success();
    }
}
